#include "SampleApp.h"
#include <Userenv.h>
#define USE_DX_11
//--------------------------------------------------------------------------------------
// Called every time the application receives a message
//--------------------------------------------------------------------------------------
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
	PAINTSTRUCT ps;
	HDC hdc;

	switch( message )
	{
	case WM_PAINT:
		hdc = BeginPaint( hWnd, &ps );
		EndPaint( hWnd, &ps );
		break;

	case WM_DESTROY:
		PostQuitMessage( 0 );
		break;

	default:
		return DefWindowProc( hWnd, message, wParam, lParam );
	}

	return 0;
}


CSampleApp::CSampleApp(void)
{
	m_hInst = NULL;
	m_hWnd = NULL;

	m_pRenderSystem = NULL;
}


CSampleApp::~CSampleApp(void)
{
	//uinit();
}

void CSampleApp::onEvent(MSG& msg)
{

}


bool CSampleApp::updateFrame(int passedTime)
{
	Render();
	static int FPS = 0;
	static int tempFPSTime = 0;
	FPS++;
	int nowtime = GetTickCount();
	int CurFPS = 0;
	if (nowtime - tempFPSTime >= 1000)
	{
		CurFPS = FPS;
		tempFPSTime = nowtime;
		FPS = 0;
		//printf("--->FPS %d\n", CurFPS);
		setFPS( CurFPS );
	}


	return true;
}

bool CSampleApp::beginLog(const wchar_t* fileName)
{
	HANDLE hToken = 0;
	OpenProcessToken(GetCurrentProcess() , TOKEN_ALL_ACCESS , &hToken);
	wchar_t _usrDir [1024]={0};
	DWORD _size = 1024;
	GetUserProfileDirectoryW(hToken,_usrDir,&_size);
	wstring userDir = std::wstring(_usrDir) + L"\\" + fileName;
	m_strLogFileName = userDir;
	LABOR_LOG_REDIR(userDir.c_str());

	return true;
}

bool CSampleApp::endLog()
{
	LABOR_LOG_FLUSH();
	LABOR_LOG_CLOSE();
	return true;
}

void CSampleApp::setFPS( float fFPS )
{
	wchar_t buf[256] = {0};
	swprintf( buf, L"%s    FPS:%f", m_strBaseWindowName.c_str(), fFPS);


	//wsprintf(buf, L"%s    FPS:%f", fFPS);
	SetWindowText(m_hWnd, buf );
}

bool CSampleApp::initEnv()
{
	return true;
}

bool CSampleApp::init( const char* appModuleFile, const wchar_t* className, const wchar_t* window_name, int width, int height )
{
#ifdef _WIN64
#ifdef _DEBUG
	GetLaborCore()->loadPlugin( L"LaborD3D11_Debug_x64.dll");
#else
	GetLaborCore()->loadPlugin( L"LaborD3D11_Release_x64.dll");
#endif
#else
#ifdef _DEBUG
	GetLaborCore()->loadPlugin( L"LaborD3D11_Debug_Win32.dll");
#else
	GetLaborCore()->loadPlugin( L"LaborD3D11_Release_Win32.dll");
#endif
#endif

	m_pRenderSystem = GetLaborCore()->getRenderSystemByName(L"D3D11 Renderer");


	GetLaborCore()->setRenderSystem( m_pRenderSystem );
	GetLaborCore()->getRenderSystem();
	if ( NULL == m_pRenderSystem )
		return 0;

	m_strBaseWindowName = window_name;
	if( FAILED( InitWindow( (HINSTANCE)m_pRenderSystem->getInst(), 0 , className, window_name, width, height) ) )
		return 0;

	char _appModuleFile[256]={0};
	strncpy_s(_appModuleFile,appModuleFile,256);
	for(int i = strlen(_appModuleFile) ; i >=0 ; i --)
	{
		if(_appModuleFile[i] == '/' || _appModuleFile[i] == '\\')
		{
			_appModuleFile[i + 1] = 0;
			break;
		}
	}

	string t_strTemp = _appModuleFile;
	//t_strTemp = t_strTemp.substr(0, t_strTemp.length()-1);
	//int t_nPos = t_strTemp.find_last_of('\\');
	//string t_strResourcePath = t_strTemp.substr(0, t_nPos+1);
	wchar_t t_strResourcePathW[4096] = {0};
	Labor_LocaleToUnicode( t_strTemp.c_str() ,t_strResourcePathW , 4096 );
	wstring t_strShaderPath = t_strResourcePathW;

	m_strCurrentPath = t_strShaderPath;
	t_strShaderPath += L"shader\\";
	m_strShaderPath = t_strShaderPath;
	m_pRenderSystem->setShaderPath( t_strShaderPath.c_str() );

	wstring t_strTexturePath = t_strResourcePathW;

	t_strTexturePath += L"resource\\texture\\";
	m_pRenderSystem->setTexturePath( t_strTexturePath.c_str() );


	if( !m_pRenderSystem->initDevice( (int)m_hWnd) )
	{
		m_pRenderSystem->cleanUpDevice();
		return 0;
	}
	initEnv();
	initRenderData();
	return true;

}


bool CSampleApp::uinit()
{

	m_pRenderSystem->cleanUpDevice();

	return true;

}

HRESULT CSampleApp::InitWindow( HINSTANCE hInstance, int nCmdShow, const wchar_t* className, const wchar_t* window_name, int width, int height )
{
	// Register class
	WNDCLASS wcex;
	wcex.style = CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc = WndProc;
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = hInstance;
	wcex.hIcon = LoadIcon( hInstance, ( LPCTSTR )IDI_APPLICATION );
	wcex.hCursor = LoadCursor( NULL, IDC_ARROW );
	wcex.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );
	wcex.lpszMenuName = NULL;
	wcex.lpszClassName = className;
	if( !RegisterClass( &wcex ) )
		return E_FAIL;

	// Create window
	m_hInst = hInstance;
	RECT rc = { 0, 0, width, height };
	AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );
	m_hWnd = CreateWindow( className, window_name, WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInstance,
		NULL );
	if( !m_hWnd )
		return E_FAIL;

	ShowWindow( m_hWnd, TRUE );

	return S_OK;
}

